import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;

// Parameters:"C:\Workspace_Fall2011\TextClassifier\Stopwords.txt""C:\Workspace_Fall2011\TextClassifier\TrialDocs\BodyOfArticle.txt"


public class StopwordRemover {

	
	static File file = null;
	static BufferedReader reader = null;
	static FileInputStream fis = null;
	static int ch;
	private static String delim = " \t\n.,:;?!-/()[]\"\'";
	protected static HashSet<String> stopWords = null;
	static StringBuffer modifiedBody = null;
//	static HashMap<String,Integer> vocabulary = new HashMap<String, Integer>();

	public StopWordRemoveResult remove(String stopwordFile, String body, ArrayList<String> vocabulary) {
		String word, line, modifiedLine;
		
		stopWords = new HashSet<String>();
		

		FileReader fr_swFile = null;
		try {
			fr_swFile = new FileReader(stopwordFile);

			BufferedReader br_swFile = new BufferedReader(fr_swFile);

			while ((word = br_swFile.readLine()) != null) {
				stopWords.add(word);
				//System.out.println(word);
			}

			
				line = body;
				//System.out.println("Line: "+line);
				StringTokenizer st = new StringTokenizer(body, delim);
				modifiedBody = new StringBuffer("");
				while (st.hasMoreTokens())
				{
					
					word = st.nextToken();
										
					
					if (!stopWords.contains(word.toLowerCase()))
					{
						if (!vocabulary.contains(word.toLowerCase()))
							vocabulary.add(word.toLowerCase());
						modifiedBody.append(word.toLowerCase());
						modifiedBody.append(" ");
						//System.out.println("modified body:"+modifiedBody);
					}
					
				}
				
				
		
			
			br_swFile.close();
	
			

		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (Exception e) {
			System.out.println(e);
		}
	
		//return modifiedBody.toString();
		return new StopWordRemoveResult(vocabulary,modifiedBody.toString());
	}



public StopWordRemoveResult remove(String stopwordFile, String body) {
	String word, line, modifiedLine;
	ArrayList<String> tokensInTestBody = new ArrayList<String>(); 
	
	stopWords = new HashSet<String>();
	

	FileReader fr_swFile = null;
	try {
		fr_swFile = new FileReader(stopwordFile);

		BufferedReader br_swFile = new BufferedReader(fr_swFile);

		while ((word = br_swFile.readLine()) != null) {
			stopWords.add(word);
			//System.out.println(word);
		}

		
			line = body;
			//System.out.println("Line: "+line);
			StringTokenizer st = new StringTokenizer(body, delim);
			modifiedBody = new StringBuffer("");
			while (st.hasMoreTokens())
			{
				
				word = st.nextToken();
									
				
				if (!stopWords.contains(word.toLowerCase()))
				{
					if (!tokensInTestBody.contains(word.toLowerCase()))
						tokensInTestBody.add(word.toLowerCase());
					modifiedBody.append(word.toLowerCase());
					modifiedBody.append(" ");
					//System.out.println("modified body:"+modifiedBody);
				}
				
			}
			
			
	
		
		br_swFile.close();

		

	} catch (FileNotFoundException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (Exception e) {
		System.out.println(e);
	}

	//return modifiedBody.toString();
	return new StopWordRemoveResult(tokensInTestBody,modifiedBody.toString());
}
}

